package com.bjpowernode;

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {



        //冒泡
        int [] data = {65,43,87,21,6,32};
//        System.out.println(Arrays.toString(Sort01(data)));
        System.out.println(Arrays.toString(selectSort(data)));
        System.out.println(binarySelect(data,6));//返回的是数组下标
    }

    //冒泡
    public static int[] Sort01(int [] array){

        if (array.length==0){
            return array;
        }
        for (int i =0;i<array.length-1;i++){
            for (int j=0;j<array.length-1-i;j++){
                if (array[j]>array[j+1]){
                    int temp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=temp;
                }
            }
        }
        return array;
    }
    //选择
    public static int[] selectSort(int[] array ){
        if (array.length==0){
            return array;
        }
        for (int i =0 ;i<array.length-1;i++){
            int min=i;
            for (int j=min+1;j<array.length;j++){
                if (array[j]<array[min]){
                    min=j;
                }
            }
            if (min!=i){
                int temp=array[i];
                array[i]=array[min];
                array[min]=temp;
            }
        }
        return array;
    }

    //二分查找
    public static int binarySelect(int [] array,int k){
        int from=0;
        int end=array.length-1;
        int mid=(from+end)/2;
        while (from<=end){
            if (k==array[mid]){
                return mid;
            }else if (k<array[mid]){
                end=mid-1;
                mid=(from+end)/2;
            }else if (k>array[mid]){
                from=mid+1;
                mid=(from+end)/2;
            }

        }
        return -1;
    }
}
